<?php
/**
 * @author Nguyễn Sơn Tùng <sontung.x305@gmail.com> <Y!M: terminator_nst> <Skype : joker.x305>
 * @version: 1.0
 * @copyright fteam.vn
 * Date: 21/01/2013
 * Time: 18:18
 */

class uploadYB extends CInputWidget {
    public $url;
    public $width;
    public $height;
    public $idButton;
    public function init(){
        $js = "
        function ShowUploadYB(url,title,w,h,id)
          {
            var input = $('#'+id).prev('input[type=text]').attr('id');
            var url = url + '?htmlId='+input;
            var left = (screen.width/2)-(w/2);
            var top = (screen.height/2)-(h/2);
            window.open(url,title,'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
          }
        ";
        Yii::app()->clientScript->registerScript('uploadYB',$js,2);
    }
    public function run(){
        list($name, $id) = $this->resolveNameID();
        $id=$id.'_YBUpload';
        if (isset($this->htmlOptions['id']))
            $id = $this->htmlOptions['id'];
        else
            $this->htmlOptions['id'] = $id;
        if (isset($this->htmlOptions['name']))
            $name = $this->htmlOptions['name'];
        else
            $this->htmlOptions['name'] = $name;

        $contHtmlOptions = $this->htmlOptions;
        $contHtmlOptions['id'] = $id . 'container';
        $inputOptions = array('id' => $id);
        echo CHtml::openTag('div', $contHtmlOptions);
            if($this->hasModel()) {
                echo CHtml::activeTextField($this->model,$this->attribute,$inputOptions);
            }else {
                echo CHtml::textField($name,$this->value,$inputOptions);
            }
            echo CHtml::link(Yii::t('uploadYB','Upload to Youtube'),'#',array('onclick'=>'js:ShowUploadYB("'.$this->url.'","'.Yii::t('uploadYB','Upload To Youtube').'",'.$this->width.','.$this->height.',this.id);','style'=>'margin: -10px 0 0 15px','class'=>'btn','id'=>$this->idButton));
        echo CHtml::closeTag('div'); // div $contHtmlOptions
    }
}